/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import DataObject.MyColumn;
import java.util.Vector;
import org.w3c.dom.Document;

/**
 *
 * @author MinhNhat
 */
public class CreateSentence extends MySentence {
    /*
     * CREATE TABLE Persons ( P_Id int, LastName varchar(255), FirstName
     * varchar(255), Address varchar(255), City varchar(255)
    )
     */

    String Table;
    Vector Columns;

    public CreateSentence(String strCreate) {
        Table = "";
        Columns = new Vector();
        strCreate = GetTableName(strCreate);
        GetColumns(strCreate);
    }

    public String GetTableName(String strCreate) {
        String s1 = strCreate.toLowerCase();
        String s2 = " table ";
        int indexOfTableName = s1.indexOf(s2);

        String s3 = "(";
        int indexOfEndTableName = s1.indexOf(s3);
        if (indexOfTableName == -1 || indexOfEndTableName == -1) {
            return strCreate;
        }

        Table = strCreate.substring(indexOfTableName + s2.length(), indexOfEndTableName).trim();

        String s4 = ")";
        int indexOfEndSentence = s1.lastIndexOf(s4);
        if (indexOfEndSentence == -1) {
            return strCreate;
        }
        return strCreate.substring(indexOfEndTableName + 1, indexOfEndSentence).trim();
    }

    public void GetColumns(String strCreate) {
        Columns = new Vector();
        String[] listColumns = strCreate.split(",");
        for (int i = 0; i < listColumns.length; i++) {
            MyColumn column = new MyColumn(listColumns[i].trim(), i);
            Columns.add(column);
        }
    }

    public static boolean isCreateSentence(String str) {
        String s1 = str.toLowerCase().trim();
        String s2 = "create ";
        int indexOfString = s1.indexOf(s2);
        if (indexOfString == 0) {
            return true;
        }
        return false;
    }

    public void Print() {
        System.out.println("I'm CreateSentence");
    }

    public boolean Excute(Document doc, Vector Tables) {
        try {
            if (isDuplicateColumn()) {
                return false;
            }

            if (isDuplicateTable(Tables) || Table.isEmpty()) {
                return false;
            }

            MyTable table = new MyTable(doc, Table, Columns);

            Tables.add(table);
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return false;
        }
        return true;
    }

    public boolean isDuplicateColumn() {
        for (int i = 0; i < Columns.size() - 1; i++) {
            MyColumn column1 = (MyColumn) Columns.get(i);
            for (int j = i + 1; j < Columns.size(); j++) {
                MyColumn column2 = (MyColumn) Columns.get(j);
                if (column1.Name.compareTo(column2.Name) == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isDuplicateTable(Vector Tables) {
        for (int i = 0; i < Tables.size(); i++) {
            MyTable tableInDB = (MyTable) Tables.get(i);
            String tableName = tableInDB.GetName();
            if (tableName.compareTo(Table) == 0) {
                return true;
            }
        }
        return false;
    }
}
